package com.hehao.java_basic.design_mode.behaviour.chain_of_responsibility.practice;

import com.hehao.common.entiy.Receipt;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * @author Damon
 * @date 2023/6/30
 * 责任链接口实现
 */
public class ReceiptHandleChain implements IReceiptHandleChain {
    private int index = 0;

    private static List<IReceiptHandler> receiptHandlerList;

    static{
        receiptHandlerList = ReceiptHandlerContainer.getReceiptHandlerList();
    }
    @Override
    public void handleReceipt(Receipt receipt) {
        if (CollectionUtils.isNotEmpty(receiptHandlerList)) {
            if (index != receiptHandlerList.size()) {
                IReceiptHandler receiptHandler = receiptHandlerList.get(index++);
                receiptHandler.handleReceipt(receipt,this);
            }
        }
    }
}
